<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 头部 -->
      <el-card class="tree-card">
        <!-- 结构内容 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />

        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 新增弹层组件 -->
    <AddDep
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>
<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
import AddDep from './components/add-dept.vue'
export default {
  components: { TreeTools, AddDep },
  data() {
    return {
      // 头部数据结构
      company: {},
      defaultProps: {
        // 表示从这个属性显示内容
        label: 'name'
      },

      // 默认不显示
      showDialog: false,
      // 记录当前的点击的node
      node: null,

      loading: false, // 控制弹出的显示和隐藏
      departs: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.loading = false
      this.departs = transListToTreeData(result.depts, '')
      // this.departs = transListToTreeData(result.depts, "");
    },

    // 触发点击添加子部门的事件
    addDepts(node) {
      // 显示弹层
      this.showDialog = true

      this.node = node
    },
    editDepts(node) {
      // 弹出层
      this.showDialog = true

      // 赋值操作的节点
      this.node = node

      // 调用获取部门详情的方法
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>
<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>

